<?php
	include "Model/Member.php";
	// Starting the session 
	session_start(); 
	if ($_REQUEST['msg'] == "logout"){
		unset($_SESSION['user']);
		header("Location: Home.php");
	}
	else if (isset($_POST['submit'])){
		if((isset($_POST['username']) && isset($_POST['password']))) {
			$user = Member::GetMemberByUsername(null, $_POST['username']);
			//$user = Member::WithParams("sumana", "mochi");
			if (is_string($user) || $user == false){
				header("Location: LogIn.php?msg=false");
			}
			else if ($_POST['username'] == $user->GetUsername() && $_POST['password'] == $user->GetPassword()){
				$_SESSION['user'] = $user->GetUsername(); 
				header("Location: Home.php");
			}
			else{
				header("Location: LogIn.php?msg=false");
			}
		}
		else{
			header("Location: LogIn.php?msg=false");
		}
	}
	else {?>
		<div id="display_loggin">
			<?php if ($_REQUEST['msg'] == "false") { echo("<p>Login failed</p>"); } ?>
		    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="SearchBoxForm">
		        <input type="text" name="username" class="loggin_input" value="username" onfocus="if (this.value == &#39;username&#39;) {this.value = &#39;&#39;;}" onblur="if (this.value == &#39;&#39;) {this.value = &#39;username&#39;;}" />
			    <br />
			    <input type="password" name="password" class="loggin_input" value="password" onfocus="if (this.value == &#39;password&#39;) {this.value = &#39;&#39;;}" onblur="if (this.value == &#39;&#39;) {this.value = &#39;password&#39;;}" />
			    <br />
			    <a href="">Register</a> <input type="submit" name="submit" id="loggin_submit" value="Log In" />
		    </form>
		</div>
	<?php } 
?>